package com.yicong.ants.utils;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

import java.util.ArrayList;

public class TextViewUtils {

    private ArrayList<SizeAndColorSpan> mStrs;

    private TextViewUtils() {
        if (null == mStrs) {
            mStrs = new ArrayList<>();
        } else {
            mStrs.clear();
        }
    }

    public static TextViewUtils getInstance() {
        return new TextViewUtils();
    }

    public TextViewUtils setSizeAndColorSpan(int color, String str, int size, boolean dip) {
        if (!TextUtils.isEmpty(str)) {
            SizeAndColorSpan sizeAndColorSpan;
            if (mStrs.size() == 0) {
                sizeAndColorSpan = new SizeAndColorSpan(size, dip, 0, color, str);
            } else {
                int y = mStrs.size();
                int startIndex = 0;
                for (int i = 0; i < y; i++) {
                    startIndex += mStrs.get(i).getStr().length();
                }
                sizeAndColorSpan = new SizeAndColorSpan(size, dip, startIndex, color, str);
            }
            mStrs.add(sizeAndColorSpan);
        }
        return this;
    }

    public SpannableStringBuilder init() {
        int length = mStrs.size();
        if (length == 0) {
            return null;
        }
        String content = mStrs.get(0).getStr();
        for (int i = 1; i < length; i++) {
            content += mStrs.get(i).getStr();
        }
        SpannableStringBuilder mStrBuilder = new SpannableStringBuilder(content);
        for (int i = 0; i < length; i++) {
            SizeAndColorSpan sizeAndColorSpan = mStrs.get(i);
            int startIndex = sizeAndColorSpan.getStartIndex();
            int lastIndex = startIndex + sizeAndColorSpan.getStr().length();
            mStrBuilder.setSpan(getSizeSpan(sizeAndColorSpan.getSize(), sizeAndColorSpan.isDip()),
                startIndex, lastIndex,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mStrBuilder.setSpan(getColorSpan(sizeAndColorSpan.getColor()),
                startIndex, lastIndex,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return mStrBuilder;
    }

    private AbsoluteSizeSpan getSizeSpan(int size, boolean dip) {
        return new AbsoluteSizeSpan(size, dip);
    }

    private ForegroundColorSpan getColorSpan(int color) {
        return new ForegroundColorSpan(color);
    }

    private static class SizeAndColorSpan {

        private int size;
        private boolean isDip;
        private int startIndex;
        private int color;
        private String str;

        public SizeAndColorSpan(int size, boolean isDip, int startIndex, int color, String str) {
            this.size = size;
            this.isDip = isDip;
            this.startIndex = startIndex;
            this.color = color;
            this.str = str;
        }

        public int getSize() {
            return size;
        }

        public boolean isDip() {
            return isDip;
        }

        public int getStartIndex() {
            return startIndex;
        }


        public String getStr() {
            return str;
        }

        public void setStr(String str) {
            this.str = str;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }
    }

}
